import requests

class Upload_Token_1:

    '''
        第一个申请接口请求，获取token请求
    '''
    def upload_token(self,data,prefix_url):
        url = prefix_url + "/loans/login/bindchannel"
        # url = ""
        param = {"qrcode_id": data["qrcode_id"],
                 "bw_apply_type": data["bw_apply_type"],
                 "name": data["name"],
                 "company_name": data["company_name"],
                 "mobile": data["mobile"],
                 "captcha_code": data["code"],
                 "agentwe_id": data["agentwe_id"],
                 "shop_id": "0",
                 "product_id": data["product_id"],
                 "idcard": data["idcard"],
                 "company_reg_no": data["company_reg_no"],
                 "tys": "0",
                 "pft": "0",
                 "pftype": "0"
                 }
        # 获取响应对象
        response = requests.post(url=url, data=param)
        response.encoding = "utf-8"
        with open("data.html","w+",encoding="utf-8") as fp:
            fp.write(response.text)
            fp.close()
        return response.json()  # 返回本次响应体对象数据


    #采集接口
    def gather(self,data,prefix_url):
        url = prefix_url + "/loans/agent/gather"
        # 回访的参考地址
        refere = prefix_url + "/loans/agent/gather?i=lUQn6NvxjmtntJ5fV910i5lo2KFo35DbxX-0xQTNJgGCNE4KgxRAwS_uHxHvNRSeFAjJUJosCz-QWKLoXYyD_w&mid=258827"
        param = {
                 "name": data["name"],
                 "company_name": data["company_name"],
                 "agentwe_id": data["agentwe_id"],
                 "idcard": data["idcard"],
                 "company_reg_no": data["company_reg_no"],
                 "url_id":"76"
                 }
        headers = {
            "Origin":prefix_url,
            "Referer":refere,
            "X-Requested-With":"XMLHttpRequest",
            "Content-Type":"application/x-www-form-urlencoded",
            "Connection":"keep-alive"
        }
        # 获取响应对象
        try:
            response = requests.post(url=url, data=param,headers = headers)
            response.encoding = "utf-8"
            return response.json()  # 返回本次响应体对象数据
        except Exception as e:
            return "接口返回数据解析报错！" + e.args.__str__()

    '''
        对当前这个采集接口，或者一个taskid
    '''
    def getTaskId(self,data):
        common_data = {"clientId": data["clientId"],
                       "finTaskId": data["finTaskId"],
                       "taxCode": data["taxCode"],
                       "token": data["token"],
                       "taxName":data["taxName"]}
        response = requests.post(
            url="https://bigdataxy.kingdee.com:7205/tcplug/tax/taxweb/login/location",
            json=common_data,  # 注意，content-Type:application/json才能这么传数据过去
            headers={"Content-Type": "application/json;charset=utf-8",
                     "Origin": "https://bigdataxy.kingdee.com:7205",
                     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0",
                     "Host": "bigdataxy.kingdee.com:7205",
                     # "Referer": ("https://bigdataxy.kingdee.com:7205/m/taxWeb/index?clientId="
                     #             + data["clientId"]
                     #             + "&taxName=" + data["taxName"]
                     #             + "&taxCode=" + data["taxCode"]
                     #             + "&operName=" + data["operName"]
                     #             + "&operNo=" + data["operNo"]
                     #             + "&scenes=&cooperatorIds=" + data["cooperatorIds"]
                     #             + "&cityCode=&token=" + data["token"]
                     #             + "&finTaskId=" + data["finTaskId"]
                     #             + "&returnUrl=http://vv.bangongyi.com/loans/login/loding?task_id="
                     #             + data["finTaskId"]
                     #             + "&tax_code=" + data["taxCode"]
                     #             + "&order_id=" + data["order_id"]
                     #             + "&product_id=" + data["product_id"]
                     #             + "&is_match=" + data["is_match"]).encode("utf-8"),
                     "Accept": "application/json, text/plain, */*",
                     "Sec-Fetch-Dest": "empty",
                     "Sec-Fetch-Mode": "cors",
                     "Sec-Fetch-Site": "same-origin"
                     })
        response.encoding = "utf-8"
        return response.json()



    '''
        同意并授权接口
    '''
    def AgreeAndAuth(self,data):
        ####################################执行同意并授权操作
        auth_url = "https://bigdataxy.kingdee.com:7205/tcplug/tax/taxweb/authorizeWithToken"
        auth_param = {"token": data["token"],
                      "taxNo": data["taxCode"],
                      "finTaskId": data["finTaskId"],
                      "taxEntName": data["taxName"],
                      "pluginType": "WEB",
                      "instIds":
                          ["cf3e4ba4-3b09-11ec-aa33-005056b65202",
                           "cf3e4ba4-3b09-11ec-aa33-005056b65202",
                           "cf3e4ba4-3b09-11ec-aa33-005056b65202",
                           "1e6aa27e-60cc-11ed-a9c8-005056a9432b"],
                      "taskId": data["taskId"],
                      "leaderInstId": "cf3e4ba4-3b09-11ec-aa33-005056b65202"
                      }
        auth_response = requests.post(url=auth_url, json=auth_param,
                                      headers={"Content-Type": "application/json;charset=utf-8"})
        auth_response.encoding = "utf-8"

        return auth_response.json()

    '''
        登陆税务局接口
        传入data数据
    '''
    def LoginInterface(self,data):
        login_url = "https://bigdataxy.kingdee.com:7205/tcplug/tax/taxweb/loginEtax"
        operation_name = data["name"]  # 法人姓名
        idcard = data["idcard"]  # 法人身份证编号
        login_param = {"clientId": data["client_id"],
                       "finTaskId": data["finTaskId"],
                       "token": data["token"],
                       "taxName": data["taxName"],
                       "taxCode": data["taxCode"],
                       "operName": operation_name,
                       "operNo": idcard,
                       "scenes": "",
                       "cityCode": data["cityCode"],  # 湖南的编号430300
                       "financeName": "",
                       "cooperatorIds": data["cooperatorIds"],
                       "phone": "",
                       "smsCode": "", "credit": "", "invoice": "",
                       "taskId": data["taskId"],
                       "replFinTaskId": "", "reportCode": "",
                       "provinceCode": data["provinceCode"],  # 湖南省份编号
                       "gsName": data["taxCode"],  # 公司名称
                       "gsPwd": data["captcha_code"]
                       }  # 公司密码
        login_response = requests.post(url=login_url, json=login_param,
                                       headers={"Content-Type": "application/json;charset=utf-8"})
        login_response.encoding = "utf-8"

        return login_response.json()







